/**
 * 贪吃蛇游戏渲染模块
 */
 class GameRenderer {
    constructor(canvas, game) {
      this.canvas = canvas;
      this.ctx = canvas.getContext('2d');
      this.game = game;
      
      // 颜色配置
      this.colors = {
        background: '#f0f0f0',
        snake: '#4CAF50',
        snakeHead: '#2E7D32',
        food: '#F44336',
        text: '#333',
        border: '#999'
      };
      
      // 字体配置
      this.fonts = {
        small: '16px Arial',
        medium: '24px Arial',
        large: '48px Arial'
      };
    }
    
    // 清除画布
    clearCanvas() {
      this.ctx.fillStyle = this.colors.background;
      this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
    }
    
    // 绘制游戏区域边框
    drawBorder() {
      this.ctx.strokeStyle = this.colors.border;
      this.ctx.lineWidth = 2;
      this.ctx.strokeRect(0, 0, this.canvas.width, this.canvas.height);
    }
    
    // 绘制蛇
    drawSnake() {
      const snake = this.game.getSnake();
      
      snake.forEach((segment, index) => {
        this.ctx.fillStyle = index === 0 ? this.colors.snakeHead : this.colors.snake;
        this.ctx.fillRect(segment.x, segment.y, this.game.gridSize, this.game.gridSize);
        
        // 绘制蛇体边框
        this.ctx.strokeStyle = '#fff';
        this.ctx.lineWidth = 1;
        this.ctx.strokeRect(segment.x, segment.y, this.game.gridSize, this.game.gridSize);
      });
    }
    
    // 绘制食物
    drawFood() {
      const food = this.game.getFood();
      
      this.ctx.fillStyle = this.colors.food;
      this.ctx.beginPath();
      this.ctx.arc(
        food.x + this.game.gridSize / 2,
        food.y + this.game.gridSize / 2,
        this.game.gridSize / 2,
        0,
        Math.PI * 2
      );
      this.ctx.fill();
    }
    
    // 绘制分数
    drawScore() {
      const { score, highScore } = this.game.getGameState();
      
      this.ctx.fillStyle = this.colors.text;
      this.ctx.font = this.fonts.small;
      this.ctx.textAlign = 'left';
      this.ctx.fillText(`分数: ${score}`, 10, 30);
      this.ctx.fillText(`最高分: ${highScore}`, 10, 60);
    }
    
    // 绘制开始界面
    drawStartScreen() {
      this.clearCanvas();
      this.drawBorder();
      
      this.ctx.fillStyle = this.colors.text;
      this.ctx.font = this.fonts.large;
      this.ctx.textAlign = 'center';
      this.ctx.fillText('贪吃蛇游戏', this.canvas.width / 2, this.canvas.height / 2 - 50);
      
      this.ctx.font = this.fonts.medium;
      this.ctx.fillText('按任意方向键开始', this.canvas.width / 2, this.canvas.height / 2 + 20);
      
      this.ctx.font = this.fonts.small;
      this.ctx.fillText('使用方向键控制移动', this.canvas.width / 2, this.canvas.height / 2 + 60);
      this.ctx.fillText('空格键暂停/继续', this.canvas.width / 2, this.canvas.height / 2 + 90);
    }
    
    // 绘制暂停界面
    drawPauseScreen() {
      this.ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
      this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
      
      this.ctx.fillStyle = '#fff';
      this.ctx.font = this.fonts.large;
      this.ctx.textAlign = 'center';
      this.ctx.fillText('已暂停', this.canvas.width / 2, this.canvas.height / 2);
      
      this.ctx.font = this.fonts.medium;
      this.ctx.fillText('按空格键继续', this.canvas.width / 2, this.canvas.height / 2 + 50);
    }
    
    // 绘制游戏结束界面
    drawGameOverScreen() {
      const { score, highScore } = this.game.getGameState();
      
      this.ctx.fillStyle = 'rgba(0, 0, 0, 0.7)';
      this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
      
      this.ctx.fillStyle = '#fff';
      this.ctx.font = this.fonts.large;
      this.ctx.textAlign = 'center';
      this.ctx.fillText('游戏结束!', this.canvas.width / 2, this.canvas.height / 2 - 60);
      
      this.ctx.font = this.fonts.medium;
      this.ctx.fillText(`您的得分: ${score}`, this.canvas.width / 2, this.canvas.height / 2);
      this.ctx.fillText(`最高分: ${highScore}`, this.canvas.width / 2, this.canvas.height / 2 + 40);
      
      this.ctx.fillText('按任意方向键重新开始', this.canvas.width / 2, this.canvas.height / 2 + 100);
    }
    
    // 渲染游戏
    render() {
      const { gameState } = this.game.getGameState();
      
      this.clearCanvas();
      this.drawBorder();
      
      if (gameState === 'ready') {
        this.drawStartScreen();
        return;
      }
      
      this.drawSnake();
      this.drawFood();
      this.drawScore();
      
      if (gameState === 'paused') {
        this.drawPauseScreen();
      } else if (gameState === 'over') {
        this.drawGameOverScreen();
      }
    }
  }